﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;
using TestOnlineProject.Models.DataAccess;
using TestOnlineProject.Common;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class ReportBLO
    {
        ReportDAO reportDAO = new ReportDAO();
        UserDAO userDao = new UserDAO();
        QuestionDAO questionDAO = new QuestionDAO();
        SectionDAO sectionDAO = new SectionDAO();
        TestDAO testDAO = new TestDAO();
        DiscussionDAO discussionDAO = new DiscussionDAO();

        public List<Report> ToList(ref List<NoModel> noList, int? page, string keyWord = "", string column = "", string sort = "")
        {
            // Violation list.
            var reports = reportDAO.Select();

            // Search by key word.
            if (keyWord.Equals(""))
            {
                reports = reports.OrderByDescending(r => r.ReportId).ToList();
            }
            else
            {
                reports = reports.Where(r => r.Message.ToLower().Contains(keyWord.ToLower())).OrderByDescending(r => r.ReportId).ToList();
            }

            // Sort by sort type and solumn name.
            switch (column + sort)
            {
                case "NoAsc":
                    reports = reports.OrderBy(v => v.ReportId).ToList();
                    break;
                case "NoDesc":
                    reports = reports.OrderByDescending(v => v.ReportId).ToList();
                    break;
                case "MessageAsc":
                    reports = reports.OrderBy(v => v.Message).ToList();
                    break;
                case "MessageDesc":
                    reports = reports.OrderByDescending(v => v.Message).ToList();
                    break;
                case "CreatedTimeAsc":
                    reports = reports.OrderBy(v => v.CreatedTime).ToList();
                    break;
                case "CreatedTimeDesc":
                    reports = reports.OrderByDescending(v => v.CreatedTime).ToList();
                    break;
                case "IsCheckedAsc":
                    reports = reports.OrderBy(v => v.IsChecked).ToList();
                    break;
                case "IsCheckedDesc":
                    reports = reports.OrderByDescending(v => v.IsChecked).ToList();
                    break;
            }

            // Generate no. List.
            foreach (var r in reports)
            {
                noList.Add(new NoModel() { Id = r.ReportId });
            }
            noList = noList.OrderBy(n => n.Id).ToList();
            for (int i = 0; i < noList.Count; i++)
            {
                noList[i].No = i + 1;
            }

            return reports;
        }

        public List<Report> ToList()
        {
            try
            {
                var reports = reportDAO.Select();
                return reports;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Report Find(int reportId)
        {
            try
            {
                var report = reportDAO.SelectByReportId(reportId);
                return report;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string Add(int sectionId, int questionId = 0, int testId = 0, int discussionId = 0, string message = "")
        {
            try
            {
                var report = new Report();

                if (questionId > 0)
                {
                    var rep = reportDAO.Select().Where(r => (r.QuestionId == questionId && r.UserId == int.Parse(HttpContext.Current.User.Identity.Name)) && r.CreatedTime.AddHours(24) > DateTime.Now).FirstOrDefault();
                    if (rep != null)
                    {
                        return "Error24H";
                    }
                    else
                    {
                        // Deactivate.
                        //var reports = questionDAO.SelectByQuestionId(questionId).Reports.OrderByDescending(r => r.ReportId).ToList();
                        //var count = 0;
                        //for (var i = 0; i < reports.Count; i++)
                        //{
                        //    if (reports[i].IsChecked)
                        //    {
                        //        break;
                        //    }
                        //    else
                        //    {
                        //        if (count < 10)
                        //        {
                        //            count++;
                        //        }
                        //        else
                        //        {
                        //            break;
                        //        }
                        //    }
                        //}
                        //if (count == 10)
                        //{
                        //    questionDAO.Deactivate(questionId);
                        //}

                        report.QuestionId = questionId;
                    }
                }
                if (testId > 0)
                {
                    var rep = reportDAO.Select().Where(r => (r.TestId == testId && r.UserId == int.Parse(HttpContext.Current.User.Identity.Name)) && r.CreatedTime.AddHours(24) > DateTime.Now).FirstOrDefault();
                    if (rep != null)
                    {
                        return "Error24H";
                    }
                    else
                    {
                        report.TestId = testId;
                    }
                }
                if (sectionId > 0)
                {
                    var rep = reportDAO.Select().Where(r => (r.SectionId == sectionId && r.UserId == int.Parse(HttpContext.Current.User.Identity.Name)) && r.CreatedTime.AddHours(24) > DateTime.Now).FirstOrDefault();
                    if (rep != null)
                    {
                        return "Error24H";
                    }
                    else
                    {
                        report.SectionId = sectionId;
                    }
                }
                if (discussionId > 0)
                {
                    var rep = reportDAO.Select().Where(r => (r.DiscussionId == discussionId && r.UserId == int.Parse(HttpContext.Current.User.Identity.Name)) && r.CreatedTime.AddHours(24) > DateTime.Now).FirstOrDefault();
                    if (rep != null)
                    {
                        return "Error24H";
                    }
                    else
                    {
                        report.DiscussionId = discussionId;
                    }
                }

                report.UserId = int.Parse(HttpContext.Current.User.Identity.Name);
                report.Message = message;
                report.CreatedTime = DateTime.Now;
                report.IsChecked = false;

                var result = reportDAO.Insert(report);
                if (result > 0)
                {
                    return "AddSuccessfully";
                }

                return "Error";
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Uncheck(string ids = "")
        {
            var spliter = new StringHelper();

            // Split id string to int array.
            int[] idInt = spliter.Split(ids);

            int result = 0;

            // Deactivate Reports.
            foreach (var i in idInt)
            {
                // Select Report by id.
                var report = reportDAO.SelectByReportId(i);

                // Check Report IsChecked?
                //if (report.IsChecked)
                //{
                    report.IsChecked = false;
                    result += reportDAO.Update(report);
                //}
            }

            return result;
        }

        public int Check(string ids = "")
        {
            var spliter = new StringHelper();

            // Split id string to int array.
            int[] idInt = spliter.Split(ids);

            int result = 0;

            // Deactivate Reports.
            foreach (var i in idInt)
            {
                // Select Report by id.
                var report = reportDAO.SelectByReportId(i);

                // Check violation IsChecked?
                //if (!report.IsChecked)
                //{
                    report.IsChecked = true;
                    result += reportDAO.Update(report);
                //}
            }

            return result;
        }

        public int Remove(string ids = "")
        {
            var spliter = new StringHelper();

            // Split id string to int array.
            int[] idInt = spliter.Split(ids);

            int result = 0;

            // Deactivate Reports.
            foreach (var i in idInt)
            {
                // Select Report by id.
                //var report = reportDAO.SelectByReportId(i);

                // Check violation IsChecked?
                //if (!report.IsChecked)
                //{
                //report.IsChecked = true;
                result += reportDAO.Delete(i);
                //}
            }

            return result;
        }

    }
}